//
//  HistoryInfoControllerViewController.m
//  healthApp
//
//  Created by william mu on 12-8-29.
//  Copyright (c) 2012年 Tencent. All rights reserved.
//

#import "HistoryInfoControllerViewController.h"
#import "PowerPlot.h"

NSInteger floatSort(id num1, id num2, void *context)
{
    float f1 = [num1 floatValue];
    float f2 = [num2 floatValue];
    if (f1 < f2)
        return NSOrderedAscending;
    else if (f1 > f2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

@interface HistoryInfoControllerViewController ()

@end

@implementation HistoryInfoControllerViewController
@synthesize chart;
@synthesize field;
@synthesize fieldName;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"历史趋势页面";
}

- (void)viewWillAppear:(BOOL) animated
{
    [m_historyData release], m_historyData = nil;
    
    [self startRequest];
}

-(void) startRequest
{
    ProtocolService* protocol = [ProtocolService sharedInstance];
    GlobalState* state = [GlobalState sharedInstance];
    
    [protocol setTarget:self];
    [protocol setAction:@selector(onGetHistoryData:)];
    [protocol getHistoryData:state.uid field:self.field];
}

-(void) onGetHistoryData:(NSString*) json
{
    m_historyData = [[json JSONValue] retain];
    
    [self initChart];
}

-(NSArray*) getArrayData
{
    NSMutableArray* ary = [NSMutableArray array];
    
    for(NSDictionary* fieldInfo in [m_historyData reverseObjectEnumerator])
    {

        NSNumber* num = [NSNumber numberWithFloat:[[fieldInfo objectForKey:self.field] floatValue]];
        NSLog(@"addr: 0x%x", num);
        [ary addObject:num];
    }
    
    return ary;
}

-(WSData*) getData
{
    NSArray* ary = [self getArrayData];
    
    WSData *result = [WSData dataWithValues:ary];
    
    return [result indexedData] ;
}



-(NARange) getDataRange
{
    NSArray* data = [self getArrayData];
    
    data = [data sortedArrayUsingFunction:floatSort context:nil];
    
    int min = [[data objectAtIndex:0] intValue];
    int max = [data.lastObject intValue];
    
    min -= 1;
    max += 1;
    //min = max == min ? min - 1 : min;
    
    return NARangeMake(min, max);
}

-(void) initChart
{
    [self.chart generateControllerWithData:nil
                                 plotClass:[WSPlotAxis class]
                                 frame:self.chart.frame];
    
    WSPlotAxis *axis = (WSPlotAxis *)[self.chart lastPlot].view;
    
    WSData* data = [self getData];
    
    
    [self.chart generateControllerWithData:data
                                 plotClass:[WSPlotData class]
                                     frame:self.chart.frame];
    WSPlotData *line1 = (WSPlotData *)[self.chart lastPlot].view;
    
    axis.axisX.axisStyle = kAxisArrowFilledHead;
    axis.axisY.axisStyle = kAxisNone;
    axis.axisY.gridStyle = kGridDotted;
    
    NSMutableArray* ticks = [NSMutableArray array];
    
    for(int i = 1; i <= 20; i++)
    {
        [ticks addObject:[NSString stringWithFormat:@"%d", i]];
    }
    
    [axis.ticksX setTickLabelsWithStrings:ticks];
    
    NARange range = [self getDataRange];
    
    [axis.ticksY autoTicksWithRange:range number:6];
    axis.ticksY.ticksDir = kTDirectionNone;
    axis.gridStrokeWidth = 1.0;
    line1.lineColor = [UIColor blueColor];
    line1.propDefault.symbolStyle = kSymbolTriangleUp;
    line1.propDefault.symbolColor = line1.lineColor;
    line1.shadowEnabled = YES;
    line1.shadowScale = 2.0;
    line1.intStyle = kInterpolationSpline;
    [self.chart autoscaleAllAxisX];
    [self.chart autoscaleAllAxisY];
    [self.chart setAllAxisLocationXD:0.0];
    [self.chart setAllAxisLocationYD:0.0];
    [self.chart setChartTitle:self.fieldName];
    
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
